import { reactive } from "vue";
//校验登入页验证码
export const validateCapture = (rule, value, callback) => {
  if (value.length < 5) {
    callback(new Error("验证码不得少于5位数"));
  } else {
    callback();
  }
};
//校验登入页用户名
export const validateusername = (rule, value, callback) => {
  let reg = /^[a-zA-Z0-9_]{3,16}$/;
  if (reg.test(value)) {
    callback();
  } else {
    callback(new Error("用户名为3到16位数字字母下划线"));
  }
};
//校验登入页密码
export const validatepassword = (rule, value, callback) => {
  if (value) {
    callback();
  } else {
    callback(new Error("请输入密码"));
  }
};
//校验手机号逻辑
export const validatephone = (rule, value, callback) => {
  let reg =
    /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;

  if (reg.test(value)) {
    callback();
  } else {
    callback(new Error("请输入正确的手机号"));
  }
};
//校验手机号验证码逻辑
export const validateCode = (rule, value, callback) => {
  if (value.length < 5) {
    callback(new Error("请输入5位验证码"));
  } else {
    callback();
  }
};
//校验sms登入逻辑
export const SmsRules = reactive({
  phone: [{ validator: validatephone, trigger: "blur" }],
  code: [{ validator: validateCode, trigger: "blur" }],
});
//校验常规登入逻辑
export const rules = reactive({
  username: [{ validator: validateusername, trigger: "blur" }],
  password: [{ validator: validatepassword, trigger: "blur" }],
  captrue: [{ validator: validateCapture, trigger: "blur" }],
});

//点击表单提交的时候
export const userSubmitForm = (succsessCallback, faildCallback) => (formEl) => {
  // console.log(formEl);

  if (!formEl) return;
  formEl.validate((valid) => {
    //如果登入成功的逻辑
    if (valid) {
      //   console.log("submit!");
      if (succsessCallback) {
        succsessCallback();
      }
    } else {
      //如果登入失败的逻辑
      //   console.log("error submit!");
      if (faildCallback) {
        faildCallback(false);
      }
      return false;
    }
  });
};
